package cevent.source.cloudcenter.business.controller.admin;

import cevent.source.cloudcenter.server.dto.*;
import cevent.source.cloudcenter.server.service.ArticleService;
import cevent.source.cloudcenter.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cevent
 * @description 文章/产品表控制层
 */
@RestController
@RequestMapping("/admin/article")
public class ArticleController {
    private static final Logger LOG = LoggerFactory.getLogger(ArticleController.class);
    //AOP日志环绕
    public static final String BUSINESS_NAME="模块管理 文章/产品表";
    @Resource
    private ArticleService articleService;

    //1.dto-article获取
    @RequestMapping("/dtoList")
    public List<ArticleDto> getDtoList() {
        return articleService.articleDtoList();
    }


    /*2.前端分页设置，后端pageInfo处理
    前端传参方式一：表单方式--》formData 前端：直接传对象   后端参数：(Dto对象)
    前端传参方式二：流的方式--》data{param1:value1,param2:vlaue2}  后端参数：(@RequestBody Dto dto对象)
     */
    @PostMapping("/pageList")
    public ResponseDataDto list(@RequestBody ArticlePageDto articlePageDto) {
        LOG.info("查看controller的articlePageDto：{}", articlePageDto);
        ResponseDataDto responseData = new ResponseDataDto();
        //判断，如果moduleId为空，拦截
        ValidatorUtil.requiredEmpty(articlePageDto.getModuleId(),"模块ID");
        articleService.getArticlePageDtoList(articlePageDto);
        responseData.setResponseData(articlePageDto);
        return responseData;
    }

    //3.新增，流方式传参，加入@RequestBody
    @PostMapping("/save")
    public ResponseDataDto save(@RequestBody ArticleModuleDto articleModuleDto) {
        LOG.info("传入的article DTO：{}", articleModuleDto);

        ValidatorUtil.requiredLength(articleModuleDto.getName(), "文章标题", 3, 255);
        ValidatorUtil.requiredLength(articleModuleDto.getSeoTitle(), "seo标题", 3, 255);
        ValidatorUtil.requiredLength(articleModuleDto.getSeoDescription(), "seo描述", 3, 2000);
        /*ValidatorUtil.requiredLength(articleDto.getArticleVideo(), "文章视频", 3, 255);
        ValidatorUtil.requiredLength(articleDto.getVideoCover(), "视频封面", 3, 255);
        ValidatorUtil.requiredLength(articleDto.getVideoTitle(), "视频标题", 3, 255);
        ValidatorUtil.requiredLength(articleDto.getVideoDesc(), "视频介绍", 3, 2000);
        ValidatorUtil.requiredEmpty(articleModuleDto.getPrice(), "价格");
        ValidatorUtil.requiredEmpty(articleModuleDto.getLevel(), "级别");*/
        ResponseDataDto responseData = new ResponseDataDto();
        articleService.save(articleModuleDto);
        responseData.setResponseData(articleModuleDto);
        return responseData;
    }

    /**
     * 指定请求的格式为Delete
     * 4.删除模块，如果为多个参数，就定义多个/{param}/{param}
     */
    @DeleteMapping("/del/{uniId}")
    public ResponseDataDto del(@PathVariable String uniId) {
        LOG.info("传入的article uniId：{}", uniId);
        ResponseDataDto responseData = new ResponseDataDto();
        articleService.delete(uniId);
        return responseData;
    }
    //根据传入的modulePathDto存储picList
    @PostMapping("/savePicList")
    public ResponseDataDto savePicList(@RequestBody ModulePicPathDto modulePicPathDto){
        ResponseDataDto responseData=new ResponseDataDto();
        responseData.setResponseData(articleService.savePicList(modulePicPathDto));
        return responseData;
    }
}
